package com.upc.feedish.ui.home;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.upc.feedish.BarActivity;
import com.upc.feedish.InternetService;
import com.upc.feedish.MainActivity;
import com.upc.feedish.R;
import com.upc.feedish.utils.HuaweiCloudTools;
import com.upc.feedish.utils.MyBroadcastReceiver;

import org.w3c.dom.Text;


public class HomeFragment extends Fragment {

//    private HomeViewModel homeViewModel;
//    private FragmentHomeBinding binding;
    private View root;
    TextView textTem;
    TextView textHum;
    TextView textLum;
    TextView textLightStatus;
    TextView textMotorStatus;
    TextView warningTextTem;
    TextView warningTextHum;
    TextView warningTextLum;
    Button buttonT;
    Button buttonH;
    Button buttonL;
    Switch sMotor;
    Switch sLight;

    MyBroadcastReceiver br;
    InternetService internetService;
    ServiceConnection conn;


    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        if(root==null){
            root=inflater.inflate(R.layout.fragment_home,container,false);
        }
        conn = new ServiceConnection() {
            @Override
            public void onServiceDisconnected(ComponentName name) {
                getActivity().unregisterReceiver(br);
            }
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                //返回一个MsgService对象
                internetService = ((InternetService.MsgBinder) service).getService();
                internetService.huaweiGetProperties();
            }
        };
        textTem = root.findViewById(R.id.temperature);
        textHum = root.findViewById(R.id.humidity);
        textLum=root.findViewById(R.id.intensity);
        textLightStatus=root.findViewById(R.id.light_status);
        textMotorStatus=root.findViewById(R.id.motor_status);
        warningTextTem=root.findViewById(R.id.warningTem);
        warningTextHum=root.findViewById(R.id.warningHum);
        warningTextLum=root.findViewById(R.id.warningLum);
        buttonT=root.findViewById(R.id.modify_tem);
        buttonH=root.findViewById(R.id.modify_hum);
        buttonL=root.findViewById(R.id.modify_lum);
        sMotor=root.findViewById(R.id.switch_dianji);
        sLight=root.findViewById(R.id.switch_light);

        Intent intentS = new Intent(getActivity(),InternetService.class);
        getActivity().bindService(intentS, conn, Context.BIND_AUTO_CREATE);

        br=new MyBroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {
                super.onReceive(context, intent);
                textTem.setText(intent.getStringExtra("temperature"));
                textHum.setText(intent.getStringExtra("humidity"));
                textLum.setText(intent.getStringExtra("luminance"));
                textLightStatus.setText(intent.getStringExtra("lightStatus"));
                textMotorStatus.setText(intent.getStringExtra("motorStatus"));
                warningTextTem.setHint("当前为:"+intent.getStringExtra("setTem"));
                warningTextHum.setHint("当前为:"+intent.getStringExtra("setHum"));
                warningTextLum.setHint("当前为:"+intent.getStringExtra("setLum"));
            }
        };
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.upc.learning.HUAWEI");
        getActivity().registerReceiver(br, intentFilter);

        return root;
    }

    @Override
    public void onStart() {
        super.onStart();
        buttonT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HuaweiCloudTools.postCommand("Set_Temperature","Setting_Temperature",warningTextTem.getText().toString());
                    }
                }).start();
            }
        });
        buttonH.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HuaweiCloudTools.postCommand("Set_Humidity","Setting_Humidity",warningTextHum.getText().toString());

                    }
                }).start();
            }
        });
        buttonL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HuaweiCloudTools.postCommand("Set_Luminance","Setting_Luminance",warningTextLum.getText().toString());
                    }
                }).start();
            }
        });

        sMotor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sMotor.setClickable(false);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if(textMotorStatus.getText().toString().equals("ON")){
                            HuaweiCloudTools.postCommand("Control_Motor","Motor","OFF");
                            try {
                                Thread.sleep(3500);
                                sMotor.setClickable(true);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else {
                            HuaweiCloudTools.postCommand("Control_Motor","Motor","ON");
                            try {
                                Thread.sleep(3500);
                                sMotor.setClickable(true);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        });
        sLight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sLight.setClickable(false);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if(textLightStatus.getText().toString().equals("ON")){
                            HuaweiCloudTools.postCommand("Control_Light","Light","OFF");
                            try {
                                Thread.sleep(3500);
                                sLight.setClickable(true);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else {
                            HuaweiCloudTools.postCommand("Control_Light","Light","ON");
                            try {
                                Thread.sleep(3500);
                                sLight.setClickable(true);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        });


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //getActivity().unbindService(conn);
    }
}